package com.uniapp.server.controller;

import com.uniapp.server.common.R;
import com.uniapp.server.model.RecentlyEntity;
import com.uniapp.server.service.RecentlyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @author cyt
 * @describe
 * @create 2021-01-15 17:35
 */
@RequestMapping("/api/recently")
@RestController
public class RecentlyController {
    @Autowired
    private RecentlyService recentlyService;

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/getPage/{page}/{size}")
    public R query(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<RecentlyEntity> servicePage = recentlyService.getPage(page, size);
        return R.ok().data("page", servicePage);
    }

    @PutMapping("/update")
    public R update(@RequestBody RecentlyEntity recentlyEntity) {
        recentlyService.update(recentlyEntity);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable Integer id) {
        recentlyService.delete(id);
        return R.ok();
    }


    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        RecentlyEntity byId = recentlyService.getById(id);
        return R.ok().data("item", byId);
    }

    @PostMapping("/add")
    public R add(@RequestBody RecentlyEntity entity) {
        recentlyService.add(entity);
        return R.ok();
    }
}
